/*************************************************************************** 
 * (c) 2006 Dmitry Osinovsky, osdmsoftware@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OsdmSoftware.PocketPicture
{
    public partial class ColorDialog: Form
    {
        Color colorEdit;
        bool initializing = false;

        public Color ColorEdit
        {
            get
            {
                int r, g, b;
                if (redBox.Text == "")
                    r = 0;
                else
                    r = Int32.Parse(redBox.Text);

                if (greenBox.Text == "")
                    g = 0;
                else
                    g = Int32.Parse(greenBox.Text);

                if (blueBox.Text == "")
                    b = 0;
                else
                    b = Int32.Parse(blueBox.Text);

                colorEdit = Color.FromArgb(r, g, b);
                return colorEdit;
            }
            set
            {
                colorEdit = value;
                int r = colorEdit.R;
                int g = colorEdit.G;
                int b = colorEdit.B;
                initializing = true;
                redBox.Text = r.ToString();
                greenBox.Text = g.ToString();
                blueBox.Text = b.ToString();
                panel1.BackColor = colorEdit;
                panel1.Refresh();
                hueSaturationControl1.SelectedColor = colorEdit;
                luminanceControl1.SelectedColor = HueSaturationControl.HSVToColor(hueSaturationControl1.SelectedHue, hueSaturationControl1.SelectedSaturation, 255);
                luminanceControl1.SelectedValue = hueSaturationControl1.SelectedValue;
                initializing = false;
            }
        }
        
        public ColorDialog(bool useHSV)
        {
            InitializeComponent();
            hueSaturationControl1.Visible = useHSV;
            luminanceControl1.Visible = useHSV;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Color c = ColorEdit;
            }
            catch
            {
                MessageBox.Show("Cannot convert to number");
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void redBox_TextChanged(object sender, EventArgs e)
        {
            if (initializing)
                return;

            try
            {
                Color c = ColorEdit;
            }
            catch
            {
                return;
            }

            panel1.BackColor = colorEdit;
            hueSaturationControl1.SelectedColor = colorEdit;
            panel1.Refresh();
        }

        private void hueSaturationControl1_SelectedColorChanged(object sender, EventArgs e)
        {
            ColorEdit = hueSaturationControl1.SelectedColor;
        }

        private void luminanceControl1_SelectedValueChanged(object sender, EventArgs e)
        {
            hueSaturationControl1.SelectedValue = (byte) luminanceControl1.SelectedValue;
            ColorEdit = hueSaturationControl1.SelectedColor;
        }
    }
}